package com.yy.xshell.utils;

import com.alibaba.fastjson2.JSONObject;
import com.yy.xshell.core.SshCore;
import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wxy
 * @version 1.0
 * @date 2024/8/16 16:35
 */
public class ContentUtil {

    final static String REG =  "\\u001b\\[\\d{1,2}([;])?(\\d{1,2})?(m)";

    public static String processMsg(String sourceMsg, String key, String userId){
        sourceMsg = sourceMsg.trim().replaceAll("\\t", "&nbsp;&nbsp;&nbsp;&nbsp;").replaceAll("\\r", "");
        JSONObject object = new JSONObject();
        String[] split = sourceMsg.split("\\n");
        String insertStr = SshCore.getInsertStr(userId, key);
        String beforStr = SshCore.getBeforStr(userId, key);
        insertStr = insertStr.substring(0, insertStr.length()-5);
        if(split[split.length-1].startsWith(insertStr)){
            object.put("insertStr", split[split.length-1]);
            SshCore.setBeforStr(userId, key, split[split.length-1]);
            StringBuilder msg = new StringBuilder();
            for(int i = 0; i< split.length-1; i++){
                if (!StringUtils.isEmpty(split[i])) {
                    msg.append(split[i]);
                    if(i != (split.length - 1)){
                        msg.append("\n");
                    }
                }
            }
            object.put("msg", msg.toString());
        }else if(sourceMsg.startsWith("cd ")){
            object.put("insertStr", split[split.length-1]);
        } else{
            object.put("msg", sourceMsg);
        }
        object.put("key", key);
        object.put("msg", processFontColorAndBackGroundAndStyle(object.getString("msg")));


        return object.toJSONString();
    }


    /**
     * 处理数据的格式问题
     * @param msg 消息内容
     * @return
     */
    public static String processFontColorAndBackGroundAndStyle(String msg){
        if(StringUtils.isEmpty(msg)){
            return null;
        }
        StringBuilder append = new StringBuilder();
        String[] split = msg.split("\\n");
        for(String block : split){
            append.append("<div>");
            String[] span = block.split("\\u001b\\[0m");
            for(String s : span){
                if(StringUtils.isNotEmpty(s)){
                    String str = findStr(s);
                    append.append(StringUtils.isEmpty(str) ? s : str);
                }
            }
            append.append("</div>");
        }
        return append.toString();
    }

    private static String processBlock(String blockStr){
        if(StringUtils.isEmpty(blockStr) || blockStr.length() < 8){
            return "";
        }
        blockStr =  blockStr.substring(2, blockStr.length()-1);
        String[] split = blockStr.split(";");
        StringBuilder style = new StringBuilder();
        for(String s : split){
            if(s.equals("0")){
                style = new StringBuilder();
            }else if("1".equals(s) || "01".equals(s)){
                //加粗文本
                style.append("font-weight: bold;");
            }else if("3".equals(s) || "03".equals(s)){
                //斜体文本
                style.append("font-style: italic;");
            }else if("4".equals(s) || "04".equals(s)){
                //斜体文本
                style.append("text-decoration: underline;");
            }
            else if("30".equals(s)){
                //黑色
                style.append("color: black;");
            }
            else if("31".equals(s)){
                style.append("color: red;");
            }
            else if("32".equals(s)){
                style.append("color: green;");
            }
            else if("33".equals(s)){
                style.append("color: yellow;");
            }
            else if("34".equals(s)){
                style.append("color: blue;");
            }
            else if("35".equals(s)){
                style.append("color: fuchsia;");
            }
            else if("36".equals(s)){
                style.append("color: aqua;");
            }
            else if("37".equals(s)){
                style.append("color: white;");
            }

            else if("40".equals(s)){
                style.append("background-color: black;");
            }

            else if("41".equals(s)){
                style.append("background-color: red;");
            }
            else if("42".equals(s)){
                style.append("background-color: green;");
            }
            else if("43".equals(s)){
                style.append("background-color: yellow;");
            }
            else if("44".equals(s)){
                style.append("background-color: blue;");
            }
            else if("45".equals(s)){
                style.append("background-color: fuchsia;");
            }
            else if("46".equals(s)){
                style.append("background-color: aqua;");
            }
            else if("47".equals(s)){
                style.append("background-color: white;");
            }
        }
        return style.toString();
    }

    private static String findStr(String s1){
        StringBuilder stringBuilder = new StringBuilder();
        Pattern pattern = Pattern.compile(REG, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(s1);
        //boolean index = a.matches(matcher);
        if(matcher.find()){
            if(matcher.start() != 0){
                stringBuilder.append("<span>");
                stringBuilder.append(s1.substring(0, matcher.start()));
                stringBuilder.append("</span>");
            }

            stringBuilder.append("<span style='");
            String style = s1.substring(matcher.start(), matcher.end());
            stringBuilder.append(processBlock(style));
            stringBuilder.append("'>");
            stringBuilder.append(s1.substring(matcher.end()));
            stringBuilder.append("</span>");
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        String a = "\\u001b[01;32m";

//        System.out.println((REG));
//        Pattern pattern = Pattern.compile(REG, Pattern.CASE_INSENSITIVE);
////        System.out.println(pattern.split(a)[0]);
//        Matcher matcher = pattern.matcher(a);
////        //boolean index = a.matches(matcher);
//        boolean b = matcher.find();
//        System.out.println(matcher);
        System.out.println(a.substring(7,a.length()-1));
    }

}
